﻿/**********************************************************************************
 * Description:     
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2008-8-5     New           DuYaoHui     Create Class
 * 
 * ********************************************************************************/
using System;

using Fjaspx.Common;
using System.Web.UI.WebControls;
using System.IO;
namespace Fjaspx.UI.Web
{
    public partial class Administrator_template_edit : AdminPage
    {
        Fjaspx.Sys.Model.ROYcmsConfig _config = Fjaspx.Config.ROYcmsConfig.Get_Config();
        Fjaspx.Sys.Bll.ROYcms_template BLL = new Fjaspx.Sys.Bll.ROYcms_template();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                SystemCms.AdminSesion();
                Fjaspx.Sys.Model.ROYcms_template Model = new Fjaspx.Sys.Model.ROYcms_template();
                if (Request["id"] != null)
                {
                    Model = BLL.GetModel(Convert.ToInt32(Request["id"] == null ? "0" : Request["id"]));
                    TextBox_title.Text = Model.name;
                    TextBox_miaoshu.Text = Model.tag;
                }
                else
                {
                    TextBox_title.Visible = false;
                    TextBox_miaoshu.Visible = false;
                    Label1.Visible = false;
                    Label2.Visible = false;
                    Label_title.Text = "默认模板编辑";
                    Label_title_content.Text = "模板名 - " + Request["type"];
                }
                try
                {

                    TextBox_HTML.Text = SystemCms.Read_File(Server.MapPath(_config.templet_root + Session["template_z_path"].ToString() + Request["type"] + Request["id"] + ".html"),_config.templet_language);
                   
                }
                catch
                {
                    TextBox_HTML.Text = Model.htmlcontent;
                }

            }
        }
        protected void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //更新数据
            Fjaspx.Sys.Model.ROYcms_template Model_update = new Fjaspx.Sys.Model.ROYcms_template();
            if (Request["id"] != null)
            {
                Model_update.bh = Convert.ToInt32(Request["id"] == null ? "0" : Request["id"]);
                Model_update.name = TextBox_title.Text;
                Model_update.tag = TextBox_miaoshu.Text;
                Model_update.htmlcontent = TextBox_HTML.Text;
                Model_update.class_name = Request["type"];
                Model_update.htmltimes = DateTime.Now;
                Model_update.y = 0;
                BLL.Update(Model_update);
            }
            //更新模板文件
            try
            {
                string path = Server.MapPath("~/" + _config.templet_root + "/" + Session["template_z_path"].ToString() + "/" + Request["type"] + Request["id"] + ".html");
                if (!Directory.Exists(Server.MapPath("~/" + _config.templet_root + "/" + Session["template_z_path"].ToString() + "/")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/" + _config.templet_root + "/" + Session["template_z_path"].ToString() + "/"));
                }
                SystemCms.CreateFile(path, TextBox_HTML.Text,_config.templet_language);
                Fjaspx.Common.SystemCms.RemoveAllCache();//清除全局缓存
            }
            catch { Response.Redirect("/administrator/Message.aspx?message=更新模板出错了！&z=no"); }
            Response.Redirect("/administrator/Message.aspx?message=更新模板成功！&z=yes");
           
        }
        void TreeBind()
        {

            //Fjaspx.Sys.Bll.ROYcms_TemplateGroup _bll = new Fjaspx.Sys.Bll.ROYcms_TemplateGroup();
            //DropDownList_z_name.DataSource = _bll.GetAllList();
            //DropDownList_z_name.DataTextField = "z_name";
            //DropDownList_z_name.DataValueField = "bh";
            //DropDownList_z_name.DataBind();
            //DropDownList_z_name.Items.Insert(0, new ListItem("◆请选择所属组◆", ""));//插入空项，此举必须放到数据绑定之后

        }
    }
}